<?php

namespace app\api\model;

use think\Db;
use think\Cache;

class PrivatechatGreet extends Common
{
    public static function initOne($user)
    {
        if (!$user || $user['sex']=='男') return false;
        $has = self::where(['user_id'=>$user['id']])->find();
        if ($has) return false;
        $count = Db::name('privatechat_zhaohu')->where(['types'=>2,'is_hidden'=>0])->cache('PrivatechatGreetinitOnecount',600)->count();
        $pn = Db::name('privatechat_zhaohu')->where(['types'=>2,'is_hidden'=>0])->limit(rand(0, $count - 3), 3)->select();
        $inserts = [];
        foreach ($pn as $key => $value) {
            $inserts[] = [
                'channel'=>2,
                'user_id'=>$user['id'],
                'content'=>$value['content'],
                'status'=>1,
                'create_time'=>time()
            ];
        }
        Db::name('privatechat_greet')->insertAll($inserts);
        Cache::rm('greetword' . $user['id'] . '&channel1');
        Cache::rm('greetword' . $user['id'] . '&channel2');
    }
}
